/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sql;

/**
 *
 * @author Guillermo
 */
public class ShellSort {
    
 // Sample of a Shell Sort
 // H. J. Bernstein, 10 November 1999, rev 28 October 2003
 
 // Accepts sequence of names as strings and returns a sorted list
 
 
 String Names[];              // The array of strings
 int NameCount=0;             // The count of strings
 int NameCapacity=0;          // The capacity of the array
 
 void add(String xstr) {
 
   // If the array is full, double the size
   // and copy to the new array
 
   if(NameCount >= NameCapacity) {
     if (NameCapacity == 0) {
       NameCapacity = 1024;
       Names = new String[NameCapacity];
     } else {
       int ii;
       int oldcap;
       String [] xx;
       oldcap = NameCapacity;
       NameCapacity *= 2;
       xx = Names;
       Names = new String[NameCapacity];
       for (ii=0; ii < NameCount; ii++) {
          Names[ii] = xx[ii];
       }
     }
   }
   Names[NameCount] = xstr;
   NameCount++;
   return;
 }
 
 String[] trim() {
   int ii;
   String [] xx;
 
   if (NameCount > 0) {
     xx = Names;
     Names = new String[NameCount];
     for (ii=0; ii < NameCount; ii++) {
       Names[ii] = xx[ii];
     }
   } else {
     Names = null;
   }
   NameCapacity = NameCount;
   return Names;
 }
 
 
 String[] SortNames( ) {
 
   // The actual sort, done case insensitive
   // Just one comb per Range
 
   int Done;
   int Range;
   int Icount;
   String Temp;
 
   Range = NameCount/2;
   while ( Range > 0 ) {
   do {
     Done = 1;
     for (Icount = Range; Icount < NameCount; Icount += Range ) {
       if (Names[Icount-Range].compareToIgnoreCase(Names[Icount]) > 0) {
         Temp = Names[Icount];
         Names[Icount] = Names[Icount-Range];
         Names[Icount-Range] = Temp;
         Done = 0;
       }
     }
   } while (Done != 1);
   Range = Range/2;
   }
 
   return trim();  
 }
}
